package com.cjgl.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cjgl.common.BaseResponse;
import com.cjgl.common.ErrorCode;
import com.cjgl.common.ResultUtils;
import com.cjgl.exception.BusinessException;
import com.cjgl.model.dto.user.UserLoginRequest;
import com.cjgl.model.dto.user.UserRequestChangePassword;
import com.cjgl.model.entity.TblAdmin;
import com.cjgl.model.entity.TblStudents;
import com.cjgl.model.entity.TblTeacher;
import com.cjgl.model.entity.TblUsers;
import com.cjgl.service.TblAdminService;
import com.cjgl.service.TblStudentsService;
import com.cjgl.service.TblTeacherService;
import com.cjgl.service.TblUsersService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static com.cjgl.constant.UserConstant.USER_LOGIN_PERMISSIONS;
import static com.cjgl.constant.UserConstant.USER_LOGIN_STATE;

/**
 * 用户接口
 */
@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {

	@Resource
	TblUsersService usersService;
	@Resource
	private TblUsersService tblUsersService;
	@Resource
	private TblStudentsService tblStudentsService;
	@Resource
	private TblTeacherService teacherService;
	@Resource
	private TblAdminService adminService;

	/**
	 * 用户登录
	 *
	 * @param userLoginRequest
	 * @param request
	 * @return
	 */
	@PostMapping("/login")
	public BaseResponse<Boolean> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
		if (userLoginRequest == null) {
			throw new BusinessException(ErrorCode.PARAMS_ERROR);
		}
		String userNo = userLoginRequest.getUserNo();
		String userPassword = userLoginRequest.getUserPassword();
		if (StringUtils.isAnyBlank(userNo, userPassword)) {
			throw new BusinessException(ErrorCode.PARAMS_ERROR);
		}
		Boolean user = tblUsersService.userLogin(userNo, userPassword, request);
		if (user) {
			return ResultUtils.success(true);
		}
		return ResultUtils.success(false);
	}


	/**
	 * 获取当前登录用户
	 *
	 * @param request
	 * @return
	 */
	@GetMapping("/get/login")
	public BaseResponse<Object> getLoginUser(HttpServletRequest request) {
		TblUsers tblUsers = tblUsersService.getLoginUser(request);
		Integer userType = tblUsers.getUserType();
		if (userType == 1) {
			QueryWrapper<TblStudents> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("studentNo", tblUsers.getUserNo());
			TblStudents tblStudents = tblStudentsService.getOne(queryWrapper);
			request.getSession().setAttribute(USER_LOGIN_STATE, tblStudents);
			request.getSession().setAttribute(USER_LOGIN_PERMISSIONS, "student");
			return ResultUtils.success(tblStudents);
		} else if (userType == 2) {
			QueryWrapper<TblTeacher> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("teacherNo", tblUsers.getUserNo());
			TblTeacher tblTeacher = teacherService.getOne(queryWrapper);
			request.getSession().setAttribute(USER_LOGIN_STATE, tblTeacher);
			request.getSession().setAttribute(USER_LOGIN_PERMISSIONS, "teacher");
			return ResultUtils.success(tblTeacher);
		} else if (userType == 3) {
			QueryWrapper<TblAdmin> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("adminNo", tblUsers.getUserNo());
			TblAdmin admin = adminService.getOne(queryWrapper);
			request.getSession().setAttribute(USER_LOGIN_STATE, admin);
			request.getSession().setAttribute(USER_LOGIN_PERMISSIONS, "admin");
			return ResultUtils.success(admin);
		}

		return ResultUtils.success(null);
	}


	/**
	 * 获取当前登录用户
	 *
	 * @param request
	 * @return
	 */
	@GetMapping("/get/logininfo")
	public BaseResponse<Object> getLoginUserinfo(HttpServletRequest request) {
		Object attribute = request.getSession().getAttribute(USER_LOGIN_STATE);
		return ResultUtils.success(attribute);
	}

	@GetMapping("/get/permissions")
	public BaseResponse<String> getUser(HttpServletRequest request) {
		String attribute = (String) request.getSession().getAttribute(USER_LOGIN_PERMISSIONS);

		return ResultUtils.success(attribute);
	}

	/**
	 * 修改用户的登陆密码
	 * (去user表修改)
	 */
	@PatchMapping
	public BaseResponse<Boolean> changeUserPassword(@RequestBody UserRequestChangePassword userRequestChangePassword, HttpServletRequest request) {
//	public BaseResponse<Boolean> changeUserPassword(@RequestBody TblUsers userRequestChangePassword, HttpServletRequest request) {


		Object attribute = request.getSession().getAttribute(USER_LOGIN_STATE);
		log.info("判断session中是否有用户" + attribute.toString());
		if (attribute == null) {
			throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
		}

		String userPassword = userRequestChangePassword.getUserPassword();
		String userNo = userRequestChangePassword.getUserNo();

		if (StringUtils.isAllEmpty(userPassword, userNo)) {
			throw new BusinessException(ErrorCode.PARAMS_ERROR);
		}

		//返回是否修改密码成功 true 成功/false 失败
		int isChanged = usersService.changeUserPassword(userNo, userPassword);
		if (isChanged > 0) {
			return ResultUtils.success(true);
		}
		return ResultUtils.success(false);
	}

	@GetMapping("/logout")
	public BaseResponse<Boolean> logout(HttpServletRequest request) {
		if (request.getSession().getAttribute(USER_LOGIN_STATE) == null) {
			throw new BusinessException(ErrorCode.OPERATION_ERROR, "未登录");
		}
		// 移除登录态
		request.getSession().removeAttribute(USER_LOGIN_STATE);
		return ResultUtils.success(true);
	}


}
